﻿using FluentValidation;
using FluentValidation.Validators;
using Nop.Plugin.Misc.Reservations.Models;
using Nop.Services.Catalog;
using System;

namespace Nop.Plugin.Misc.Reservations.Validators
{
    public class ReservationDailyValidator : AbstractValidator<ReservationViewModel>
    {
        public ReservationDailyValidator()
        {
            RuleSet("Get", HttpGetReservationDailyRules());
            RuleSet("Post", HttpPostReservationDailyRules());
            RuleSet("AddToCart", AddToCartReservationDailyRules());
        }

        private Action HttpGetReservationDailyRules()
        {
            return () =>
                {
                    //RuleFor(r => r.CurrentCustomer)
                    //    .NotNull()
                    //    .WithMessage("Current customer should not be null");
                    //RuleFor(r => r.SelectedProduct)
                    //    .NotNull()
                    //    .WithMessage("Product should not be null");
                    //RuleFor(r => r.Quantity)
                    //    .GreaterThan(0)
                    //    .WithMessage("Invalid quantity value");
                    //RuleFor(r => r.Quantity)
                    //    .SetValidator(new QuantityMustNotBeZeroValidator());
                };
        }

        private Action HttpPostReservationDailyRules()
        {
            return () =>
                {
                    //RuleFor(r => r.Quantity)
                    //    .GreaterThan(0)
                    //    .WithMessage("Invalid quantity value");
                    RuleFor(r => r.Quantity)
                        .SetValidator(new QuantityMustNotBeZeroValidator());
                    //RuleFor(r => r.Quantity)
                    //    .Must((model, quantity) => model.SelectedReservations.Count > quantity)
                    //    .WithMessage("Selected reservation should not be greater than the quantity selected");
                };
        }

        private Action AddToCartReservationDailyRules()
        {
            return () =>
                {
                    //RuleFor(r => r.SelectedReservations)
                    //    .NotEmpty()
                    //    .WithMessage("You have not selected any reservation");
                };
        }
    }

    internal class QuantityMustNotBeZeroValidator : PropertyValidator
    {
        public QuantityMustNotBeZeroValidator()
            : base("Invalid quantity value")
        {
        }

        protected override bool IsValid(PropertyValidatorContext context)
        {
            int quantityValue = (int)context.PropertyValue;

            return quantityValue > 0;
        }
    }
}
